package cn.bonoon.kernel.web.html.form;

import cn.bonoon.kernel.descriptors.UIPropertyAccessor;
import cn.bonoon.kernel.web.annotations.form.PropertyAppend;
import cn.bonoon.kernel.web.annotations.form.PropertyAppend.AppendType;
import cn.bonoon.kernel.web.annotations.form.PropertyDetail;
import cn.bonoon.kernel.web.annotations.form.PropertyDetail.DetailType;
import cn.bonoon.kernel.web.html.FormSetting;
import cn.bonoon.kernel.web.html.UIPropertyBuilder;
/**
 * <pre>
 * 支持了查看详细信息的时候，以图片的方式显示或直接下载的方式。
 * </pre>
 * @author jackson
 *
 */
public final class PropertyDetailParser extends AbstractDetailablePropertyParser<FormSetting> {
	private final PropertyAppend[] appends;
	private final DetailType detailType;

	public PropertyDetailParser(FormSetting formParser, UIPropertyAccessor accessor, PropertyDetail detail) {
		super(formParser, accessor);
		name = detail.name();
		unit = detail.unit();
		ordinal = detail.value();
		height = detail.height();
		colspan = detail.colspan();
		rowspan = detail.rowspan();
		align = detail.align();
		showHead = detail.showHead();
		appends = detail.append();
		detailType = detail.detailType();
		init();
	}
	
	public PropertyDetailParser(FormSetting formParser, UIPropertyAccessor accessor){
		super(formParser, accessor);
		ordinal = 10;
		appends = null;
		detailType = DetailType.TEXT;
		init();
	}
	
	@Override
	protected void init() {
		super.init();
		
		addHelper(accessor);
		String fn = accessor.getName();
		if (height <= 0 && "remark".equals(fn) || "des".equals(fn)){
			height = 120;
		}
	}

	@Override
	protected void buildContent(UIPropertyBuilder pb) {
		StringBuilder html = pb.getInitHtml();
		String fieldName = accessor.getName();
		String bindedName = pb.getBindedName();
		String fn = bindedName + '.' + fieldName;
		switch (detailType) {
		case IMAGE:{
			html.append("#if($").append(fn).append(" && !$").append(fn).append(".empty)<img src='${").append(fn).append("}' height='").append(height).append("' />#end ");
			break;
		}
		case DOWNLOAD:{//默认认为这种情况下，这个路径一定为字符串
			//这里也需要考虑另一种情况，就是使用分布式文件系统的情况
			html.append("#if($").append(fn).append(" && !$").append(fn).append(".empty)<a target='_blank' href='${").append(fn).append("}'>下载</a>#end ");
			break;
		}
		default:
			//TODO 将在下个版本加入
//			if(accessor.is(String.class)){
//				EnumTextProperty etp = accessor.getAnnotation(EnumTextProperty.class);
//				if(null != etp && StringHelper.isNotEmpty(etp.value())){
//					fn += '.' + etp.value();
//				}
//			}
			html.append("$!{").append(fn).append('}');
			break;
		}
		// 只有在readonly的情况下，后面才能追加其它属性
		if (null != appends){
			for (PropertyAppend pa : appends) {
				AppendType pat = pa.type();
				if(pat == AppendType.TEXT){
					html.append(pa.value());
				}else{
					html.append(pat.getSymbol()).append("$!{").append(bindedName).append('.').append(pa.value()).append('}');
					
					switch (pat) {
					case BRACKETS:
						html.append(')');
						break;
					case SQUARE:
						html.append(']');
						break;
					case BRACES:
						html.append('}');
						break;
					case ANGLE:
						html.append("&gt;");
						break;
					}
				}
			}
		}
	}
}
